﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace GurLoader.Service
{
    public class CommandProgressEx : CommandProgress, ICommandProgressEx
    {
        protected IDispatcher Dispatcher { get; private set; }

        public CommandProgressEx(
            Command command, int progressPercentage, string status, List<CommandProgress> children, IDispatcher dispatcher)
            : base(command, progressPercentage, status)
        {
            if (dispatcher == null)
                throw new ArgumentNullException("dispatcher");

            if (children == null)
                throw new ArgumentNullException("children");

            Dispatcher = dispatcher;
            Children = new ObservableCollection<CommandProgress>(children);
        }

        public ObservableCollection<CommandProgress> Children { get; private set; }

        public void AddChild(CommandProgress progress)
        {
            if (progress == null)
                return;

            Dispatcher.Invoke(
                new Action<CommandProgress>((obj) => Children.Add(obj)), DispatcherPriority.Normal, progress);
        }

        public void RemoveChild(CommandProgress progress)
        {
            if (progress == null)
                return;

            Dispatcher.Invoke(
                new Action<CommandProgress>((obj) => Children.Remove(obj)), DispatcherPriority.Normal, progress);
        }
    }
}